<?php
/**
*   +----------------------------------------------------------------------
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------
*   | Creater Time : 2013-4-3
*   +----------------------------------------------------------------------
*   | Link ( http://www.goshopx.com  http://www.shopingj.com )
*   +----------------------------------------------------------------------
**/

	class BrandAction extends BaseAction {
		function _initialize(){
			parent::_initialize();
			$this->dao = M('ProductBrand');
			$status = array('-1'=>'过期品牌','0'=>'不显示品牌','1'=>'显示品牌');
			$this->assign('status',$status);
// 			dump($this->brand->where(1)->select());
		}
		/**
		 * (non-PHPdoc)
		 * @see BaseAction::index()
		 */
		function index () {
			$dao=$this->dao;
			/* $condition=$this->create_where();
			$condition=array_merge($condition,$maps); */
			$condition = $_REQUEST['keywords'] ? array('name'=>array('like','%'.$_REQUEST['keywords'].'%')) : array();
			$count=$dao->where($condition)->count();
			$this->assign("totalCount",$count);
			if($count>0){
				$page=new Page($count,$this->listrows);
				$list=$dao->where($condition)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
				//echo($dao->getlastsql());
				$this->assign('list',$list);
			}
			$ids = '0';$pn = array();
			foreach ( $list as $val ) {
				$ids .= ','. $val['id'];
			}
			$ree = M()->query( "select brand_id,status,count(id) num from product where brand_id in ({$ids}) group by brand_id,status" );
			//dump($ree);
			foreach ( $ree as $val) {
				if ( $val['status']==1 ) $pn[$val['brand_id']] = array('sj'=>$val['num']);
				else $pn[$val['brand_id']] = array('xj'=>$val['num']);
			}
			$this->assign('brandp',$pn);
			$this->display();		
		}
		
		/**
		 * 插入前添加时间
		 */
		function _before_insert(){
			$_POST['create_uid'] = $_POST['write_uid'] = $_SESSION['userid'];
			$_POST['create_date'] = $_POST['write_date'] = time();
		}
		
		/**
		 * 更新前添加时间
		 */
		function _before_update(){
			$_POST['create_uid'] = $_SESSION['userid'];
			$_POST['create_date'] = time();
		}
		function del(){
			parent::delall($this->dao);
		}
		
		/**
		 * 插入后更新缓存
		 */
		function _after_insert(){
			parent::_cache(1,'brand');
		}
		/**
		 * 更新
		 */
		function update(){
			parent::update($this->dao,U('Brand/index'));
		}
		/**
		 * 更新后更新缓存
		 */
		function _after_update(){
			parent::_cache(1,'brand');
		}
		
		/*
		 * 查找
		 * */
		function lookup(){
			if($_POST['name']){
				$maps['name'] = array('LIke','%'.$_POST['name'].'%');
			}
			
			$this->assign('name',$_POST['name']);
			parent::lookup($maps);
			
		}
		
	}